const util = require('../../utils/util.js')
const WxParse = require('../wxParse/wxParse.js');
const setText = (context, fs, color, x, y, c) => {
	context.setFontSize(fs);
	context.setFillStyle(color);
	context.setTextAlign('left');
	context.fillText(c, x, y);
	context.restore();
};
Page({
	data: {
		logs: '',
		maskHidden: false,
		judge: '',
		http: getApp().globalData.map,
		state: 1,
		shoucang: '',
		uid: '',
		goodid: '',
		tap: 0,
		list: '',
		tz: '',
		tjr: '',
		num: 1,
		tshu:'',
		xz:0,
		shifo:true
	},
	onShow(e) {
		let that = this
		console.log(e)
		wx.getStorage({
			key: 'uid',
			success: (t) => {
				wx.request({
					url: getApp().globalData.http + 'action=getWXACodeUnlimit&uid=' + t.data.uid + '&gid=' + this.data.goodid +
						'&type=goods',
					success: (e) => {
						console.log(e)
						let flight = that.data.flightDetail
						let fsm = wx.getFileSystemManager(); //文件管理器
						let FILE_BASE_NAME = 'delay_certificate_'; //自定义文件名
						let buffer = wx.base64ToArrayBuffer(e.data.data.buffer); //转为Buffer数据
						const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.png`; //文件路径
						fsm.writeFile({ //把图片从本来拿到
							filePath,
							data: buffer,
							encoding: 'binary',
							success() {
								that.setData({
									imgDataUrl: filePath,
								})
							},
						});
						that.setData({
							imgurl: "data:image/png;base64," + e.data.data.buffer
						})
					}
				})
				if (t.data.Vip == '1') {
					this.setData({
						judge: '赚'
					})
				}
				this.a()
				this.setData({
					uid: t.data
				})
			},
			fail: () => {
				this.a()
			}
		})
	},
	a: function() {
		let _this = this
		console.log(this.data.uid.uid)
		let b = ''
		if (this.data.uid.uid != undefined) {
			b = getApp().globalData.http + "action=showGoods&goodid=" + this.data.goodid + '&uid=' + this.data.uid.uid +
				'&tjrid=' + this.data.tjr
		} else {
			b = getApp().globalData.http + "action=showGoods&goodid=" + this.data.goodid
		}
		wx.request({
			url: b,
			success: (res) => {
				console.log(res)
				wx.downloadFile({
					url: this.data.http + res.data.data.goods.albums[0].original_path, //网络路径
					success: function(res3) {
						//背景图
						_this.setData({
							bgt: res3.tempFilePath
						})
					}
				})
				console.log(res)
				let a1 = 0
				if (res.data.data.goods.SepcList.length > 0) {
					a1 = res.data.data.goods.SepcList[0].ID
				}
				_this.setData({
					logs: res.data.data,
					spid: a1,
					shoucang: res.data.data.shoucang,
					tap: res.data.data.shopCar,
				})

				let a = res.data.data.goods.content
				let b = a.replace(/="/g, '="https://gxyc.jxcqs.com')
				var temp = WxParse.wxParse('article', 'html', b, this, 5)
			}

		})
	},
	gb(e) {
		this.setData({
			spid: e.currentTarget.dataset.id
		})
	},
	sta(){
		wx.getStorage({
			key: 'car',
			success: (e) => {
				console.log(e)
				if(e.data==''){
					this.setData({
						shifo:false
					})
				}else{
					this.setData({
						isMr: e.data[0].isMr,
						car: e.data
					})
					wx.request({
						url: getApp().globalData.http + 'action=beforeAddCar&goodid=' + this.data.goodid + '&uid=' + this.data.uid
							.uid+'&carid='+e.data[this.data.xz].ID,
						success: (e) => {
							console.log(e)
							this.setData({
								issp:e.data.data.issp
							})
						}
					})
				}
			}
		})
	},
	onLoad: function(e) {
		this.setData({
			goodid: e.id
		})
		if (e.tjr != undefined) {
			wx.setStorage({
				key: 'tjrid',
				data: e.tjr
			})
			this.setData({
				tjr: e.tjr
			})
		}
		wx.getStorage({
			key: 'uid',
			success: (t) => {
				this.setData({
					uid: t.data,
				})
				this.sta()
			}
		})
	},
	sure() {
		if (this.data.uid.uid == undefined) {
			wx.navigateTo({
				url: '../register/register'
			})
		} else {
			if (this.data.list == 0) {
				wx.request({
					url: getApp().globalData.http + 'action=addShopCar&goodid=' + this.data.goodid + '&userid=' + this.data.uid.uid +
						'&spid=' + this.data.spid + '&nums=' + this.data.num,
					success: (e) => {
						console.log(e)
						if (e.data.code == 0) {
							wx.showToast({
								title: e.data.msg,
								icon: 'none'
							})
						} else {
							this.setData({
								tap: e.data.data.shopCar,
								state: 1
							})
						}
					}
				})
			} else {
				console.log(this.data.logs)
				this.setData({
					state: 1
				})
				wx.navigateTo({
					url: "../place/place?name=" + this.data.goodid + "&num=" + this.data.num + '&spid=' + this.data.spid
				})
			}
		}

	},
	plusn() {
		this.setData({
			num: this.data.num + 1
		})
	},
	subtract() {
		if (this.data.num > 1) {
			this.setData({
				num: this.data.num - 1
			})
		}
	},
	none(e) {
		this.setData({
			state: e.currentTarget.dataset.id,
			list: e.currentTarget.dataset.shopping,
			tshu:0
		})
	},
	add() {
		if (this.data.uid != '') {
			wx.request({
				url: getApp().globalData.http + 'action=addShopSC&userid=' + this.data.uid.uid + '&goodid=' + this.data.goodid,
				success: (e) => {
					wx.showToast({
						icon: 'none',
						title: e.data.msg
					})
					this.setData({
						shoucang: 1
					})
				}
			})
		} else {
			wx.navigateTo({
				url: '../register/register'
			})
		}
	},
	qx() {
		if (this.data.uid != '') {
			wx.request({
				url: getApp().globalData.http + 'action=qxShopSC&userid=' + this.data.uid.uid + '&goodid=' + this.data.goodid,
				success: (e) => {
					wx.showToast({
						icon: 'none',
						title: e.data.msg
					})
					this.setData({
						shoucang: 0
					})
				}
			})
		} else {
			wx.navigateTo({
				url: '../register/register'
			})
		}
	},
	onShareAppMessage: function(options) {
		return {
			title: this.data.logs.goods.goods_name,
			desc: '分享页面的内容',
			path: '/pages/goods/goods?id=' + this.data.goodid + '&tjr=' + this.data.uid.uid // 路径，传递参数到指定页面。
		}
	},
	previewImage: function(e) {
		var current = e.currentTarget.dataset.id;
		var imgList = e.currentTarget.dataset.src
		let a = []
		let that = this
		imgList.forEach(function(item, index) {
			a.push(that.data.http + item.original_path)
		})
		console.log(a)
		//图片预览
		wx.previewImage({
			current: current, // 当前显示图片的http链接
			urls: a // 需要预览的图片http链接列表
		})
	},
	createNewImg: function(e) {
		console.log(this.data.bgt)
		let that = this
		var ctx = wx.createCanvasContext('mycanvas');
		var c_avatar = this.data.bgt;
		var wechat = this.data.imgDataUrl;
		// 绘制背景颜色
		ctx.fillStyle = "#fff";
		ctx.fillRect(0, 0, 375, 682);
		// 绘制孩子头像、姓名  年份
		//绘制名字
		setText(ctx, 16, '#333333', 90, 45, that.data.uid.nick_name);
		//绘制年龄
		setText(ctx, 14, '#333333', 90, 65, '手机号' + that.data.uid.mobile);
		// 绘制画报外框
		ctx.rect(20, 85, 335, 457)
		ctx.setFillStyle('white')
		ctx.fill()
		// 绘制画报背景图
		//这个地方的图片是需要注意，图片需要下载不然，手机上不能正常显示！！！
		ctx.drawImage(c_avatar, 30.85, 95, 315, 528);
		//头像
		ctx.arc(45, 45, 25, 0, 2 * Math.PI) //画出圆
		ctx.strokeStyle = "#fff";
		ctx.clip(); //裁剪上面的圆形

		ctx.drawImage(wechat, 20, 20, 50, 50);
		// 绘制生成画报
		ctx.draw(true, setTimeout(function() {
			wx.canvasToTempFilePath({
				canvasId: 'mycanvas',
				success: function(res) {
					console.log(res)
					var tempFilePath = res.tempFilePath;
					that.setData({
						imagePath: tempFilePath
					});
				},
				fail: function(res) {
					console.log(res);
				}
			})
		}, 1000));
	},
	//点击保存到相册
	baocun: function() {
		var that = this
		wx.saveImageToPhotosAlbum({
			filePath: that.data.imagePath,
			success(res) {
				wx.showModal({
					content: '海报已保存到相册',
					showCancel: false,
					confirmText: '确定',
					confirmColor: '#333',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							/* 该隐藏的隐藏 */
							that.setData({
								maskHidden: false
							})
						}
					},
					fail: function(res) {
						console.log(11111)
					}
				})
			}
		})
	},
	//点击生成
	formSubmit: function(e) {
		var that = this;
		this.setData({
			maskHidden: false
		});
		wx.showToast({
			title: '海报生成中...',
			icon: 'loading',
			duration: 1000
		});
		wx.downloadFile({
			url: e.currentTarget.dataset.id, //网络路径
			success: function(res3) {
				//背景图
				that.setData({
					path: res3.tempFilePath
				})
			}
		})
		setTimeout(function() {
			wx.hideToast()
			that.createNewImg();
			that.setData({
				maskHidden: true
			});
		}, 1000)
	},
	che(){
		this.setData({
			tshu:1,
		})
	},
	xc(){
		this.setData({
			tshu:0,
		})
	},
	xz(e){
		console.log(e)
		this.setData({
			xz:e.detail.value,
			tshu:0,
		})
		this.sta()
	}
})
